package gameEngine.com.Actor;

import gameEngine.com.GameLayer;
import gameEngine.com.GameLib;
import gameEngine.com.IConstant;
import gameEngine.com.MainActivity;
import gameEngine.com.Sprite;
import gameEngine.com.state.StateGameplay;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class TankMC extends Actor implements IConstant {

	public static final int MAX_BLOOD = 100;
	public static final int MAX_MANA = 100;

	public static int MOVE_DOWN_LEFT = 0;
	public static int MOVE_DOWN = 1;
	public static int MOVE_DOWN_RIGHT = 2;
	public static int MOVE_RIGHT = 3;
	public static int MOVE_UP_RIGHT = 4;
	public static int MOVE_UP = 5;
	public static int MOVE_UP_LEFT = 6;
	public static int MOVE_LEFT = 7;

	public int m_blood = MAX_BLOOD / 2;
	public int m_mana = MAX_MANA / 3;
	public static int stateMCMove = -1;
	public static int angle = 0;
	public static int alignRotationAngle = 90;// tuy thuoc vao hinh tren sprite neu nam ngam ben trai thi + 0, huong len thi +90
	public static int MC_SPEED_XY = 8;
	public static int MC_SPEED_X = 8;
	public static int MC_SPEED_Y = 8;
	public static final int STATE_IDE = 0;
	public static final int STATE_AUTO_MOVE = 1;
	public static final int STATE_TRACK_MC_MOVE = 2;
	public static final int STATE_ATTACK_MC = 3;
	public static final int STATE_HURT_MC = 4;
	public static final int STATE_DIE = 5;
	public TankMC(int x, int y, int w, int h, String string, String string2) {
		// TODO Auto-generated constructor stub
		super(x, y, w, h, string, string2);
		m_speedx = MC_SPEED_X;
		m_speedy = MC_SPEED_Y;
		m_speedxy = MC_SPEED_XY;
	}

	@Override
	public void render() {
		// sprite.drawAFrame(MainActivity.mainCanvas, 0, m_x, m_y);
		
		sprite.drawAnim(MainActivity.mainCanvas, angle, 1, 1, (m_x + GameLayer.screenOffsetX), (m_y + GameLayer.screenOffsetY));
		MainActivity.mainPaint.setColor(Color.RED);
		MainActivity.mainCanvas.drawRect(m_x + GameLayer.screenOffsetX, m_y + GameLayer.screenOffsetY, m_x + 2 + GameLayer.screenOffsetX, m_y + 2 +GameLayer.screenOffsetY, MainActivity.mainPaint);
		MainActivity.fontsmall.drawString(GameLib.mainCanvas, " " + angle, (m_x + GameLayer.screenOffsetX) - 10, (m_y + GameLayer.screenOffsetY) - 15, 0);
		drawRect(m_x+ GameLayer.screenOffsetX,m_y+ GameLayer.screenOffsetY,m_Width ,m_Height );
	}

	public void UpdateCameraOffset() {
		GameLayer.screenOffsetX = (GameLib.SCREEN_WIDTH >> 1) - m_x;
		GameLayer.screenOffsetY = (GameLib.SCREEN_HEIGHT >> 1) - m_y;
		if (GameLayer.screenOffsetX > 0) // vi nguoc voi char
			GameLayer.screenOffsetX = 0;
		else if (GameLayer.screenOffsetX < -(GameLayer.MAP_WIDTH_PIXEL - (GameLib.SCREEN_WIDTH)))
			GameLayer.screenOffsetX = -(GameLayer.MAP_WIDTH_PIXEL - (GameLib.SCREEN_WIDTH));

		if (GameLayer.screenOffsetY > 0)
			GameLayer.screenOffsetY = 0;
		else if (GameLayer.screenOffsetY < -(GameLayer.MAP_HEIGHT_PIXEL - (GameLib.SCREEN_HEIGHT)))
			GameLayer.screenOffsetY = -(GameLayer.MAP_HEIGHT_PIXEL - (GameLib.SCREEN_HEIGHT));

	}


	public void update() {
		if ((m_x + GameLayer.screenOffsetX < -50) || ((m_x + GameLayer.screenOffsetX) > GameLib.SCREEN_WIDTH)
				|| (m_y + GameLayer.screenOffsetY < -50) || ((m_y + GameLayer.screenOffsetY) > GameLib.SCREEN_HEIGHT))
			return;
		switch (state) {
		case STATE_IDE:

			break;
		case STATE_AUTO_MOVE:
		
			break;
		case STATE_TRACK_MC_MOVE:
			int pointMove = -1;
			int pointBullet = -1;
			for (int i = 0; i < GameLib.m_currentNumPointer; i++) {
				if (GameLib.checkPointInCicle(GameLib.arraytouchPosX[i], GameLib.arraytouchPosY[i], GameLib.s_dpadCenterX, GameLib.s_dpadCenterY, GameLib.DPAD_LENGTH))
				{
					if (pointMove <0  &&
							(GameLib.arraytouchState[i] == MotionEvent.ACTION_MOVE || GameLib.arraytouchState[i] == MotionEvent.ACTION_DOWN))
					{
						pointMove = i;
						moveByDpad(GameLib.arraytouchPosX[i], GameLib.arraytouchPosY[i]);
					Log.d("Move", "Moveeeeeee");
					}
				}
				
				if (!GameLib.checkPointInCicle(GameLib.arraytouchPosX[i], GameLib.arraytouchPosY[i], GameLib.s_dpadCenterX, GameLib.s_dpadCenterY, GameLib.DPAD_LENGTH))
					if (pointBullet < 0 && ( GameLib.arraytouchState[i] == MotionEvent.ACTION_UP || GameLib.arraytouchState[i] == MotionEvent.ACTION_POINTER_UP))
					{
						pointBullet = i;
						StateGameplay.listBullet.add(Bullet.creaBullet(this, GameLib.arraytouchPosX[i], GameLib.arraytouchPosY[i], false, true));
					}
				}			

			getDirectionbyKey();
			UpdateCameraOffset();
			break;
		case STATE_ATTACK_MC:

			break;
		case STATE_HURT_MC:

			break;
		case STATE_DIE:
			if (sprite.hasAnimationFinished()) {
				state = -1;
			}
			break;
		default:
			break;
		}

	}

	public void getDirectionbyKey() {
		if (GameLib.isKeyHold(KeyEvent.KEYCODE_4) || GameLib.isKeyHold(KeyEvent.KEYCODE_DPAD_LEFT)) {
			if (stateMCMove != MOVE_LEFT) {
				angle = 180 + alignRotationAngle;
				// sprite.setAnim(MOVE_LEFT, true, false);
				stateMCMove = MOVE_LEFT;
			}
			if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size() - 1), m_x - MC_SPEED_XY, m_y, 32, 32)) {
				m_x -= TankMC.MC_SPEED_XY;
			}
			// if
			// (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size()
			// - 1), m_x, m_y + offy, 32, 32)) {
			// m_y += offy;
			// }
		} else if (GameLib.isKeyHold(KeyEvent.KEYCODE_6) || GameLib.isKeyHold(KeyEvent.KEYCODE_DPAD_RIGHT)) {
			angle = 0 + alignRotationAngle;
			if (stateMCMove != MOVE_RIGHT) {
				// sprite.setAnim(MOVE_RIGHT, true, false);
				stateMCMove = MOVE_RIGHT;
			}
			if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size() - 1), m_x + MC_SPEED_XY, m_y, 32, 32)) {
				m_x += TankMC.MC_SPEED_XY;
			}
		} else if (GameLib.isKeyHold(KeyEvent.KEYCODE_2) || GameLib.isKeyHold(KeyEvent.KEYCODE_DPAD_UP)) {
			if (stateMCMove != MOVE_UP) {
				angle = -90 + alignRotationAngle;
				// sprite.setAnim(MOVE_UP, true, false);
				stateMCMove = MOVE_UP;
			}
			if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size() - 1), m_x, m_y - MC_SPEED_XY, 32, 32)) {
				m_y -= TankMC.MC_SPEED_XY;
			}
		} else if (GameLib.isKeyHold(KeyEvent.KEYCODE_8) || GameLib.isKeyHold(KeyEvent.KEYCODE_DPAD_DOWN)) {
			if (stateMCMove != MOVE_DOWN) {
				angle = 90 + alignRotationAngle;
				// sprite.setAnim(MOVE_DOWN, true, false);
				stateMCMove = MOVE_DOWN;
			}
			if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size() - 1), m_x, m_y + MC_SPEED_XY, 32, 32)) {
				m_y += TankMC.MC_SPEED_XY;
			}
		}
	}

	public int moveByDpad( int _touchPosX, int _touchPosY) {

		
		int offx = (_touchPosX - GameLib.s_dpadCenterX) * MC_SPEED_XY / GameLib.DPAD_LENGTH;
		int offy = (_touchPosY - GameLib.s_dpadCenterY) * MC_SPEED_XY / GameLib.DPAD_LENGTH;
		if (offx == 0 && offy == 0)
			return 0;

		int line_B_Pow2 = (offx * offx + offy * offy);
		double linec = Math.sqrt(line_B_Pow2);
		double line_A_Pow2 = ((offx + linec) * (offx + linec) + offy * offy);
		double cosA = -(line_A_Pow2 - 2 * line_B_Pow2) / (2 * line_B_Pow2);

		double radian = GameLib.getRadians(0, 0, _touchPosX - GameLib.s_dpadCenterX, _touchPosY - GameLib.s_dpadCenterY);
		angle = GameLib.getDegrees(radian) + alignRotationAngle;// cong tuy theo module. neu dugn huong len +90, nam ngang huong phai +0
		// angle = (int) (Math.acos(cosA) * 180 / GameLib.PI);
		// Log.d("   ","   " + angle);
		//mcObject._x *speed
		//		mcObject._y += Math.sin(dir*Math.PI/180)*speed
		if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size() - 1), m_x + offx, m_y, 32, 32)) {
			//	m_x += offx;
			m_x += MC_SPEED_XY * Math.cos(GameLib.changeDegreeToRadian(angle - 90));
		}
		if (!GameLayer.checkCollisionPhysic(GameLayer.arrayListDataMap.get(GameLayer.arrayListDataMap.size() - 1), m_x, m_y + offy, 32, 32)) {
			//	m_y += offy;
			m_y += MC_SPEED_XY * Math.sin(GameLib.changeDegreeToRadian(angle - 90));
		}
		return 0;
	}

}
